package com.tedu.egmvcboot.controller;

import com.tedu.egmvcboot.mapper.ArticleMapper;
import com.tedu.egmvcboot.pojo.dto.ArticleDTO;
import com.tedu.egmvcboot.pojo.entity.Article;
import com.tedu.egmvcboot.pojo.entity.User;
import com.tedu.egmvcboot.pojo.vo.UserVO;
import com.tedu.egmvcboot.result.JsonResult;
import com.tedu.egmvcboot.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags = "文章模块")
@Validated
public class ArticleController {
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping("/article")
    @ApiOperation("发表文章")
    public JsonResult insertArticle(@RequestBody @Validated ArticleDTO articleDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("userVO");
        //log.debug("userVO:"+userVO);
        if (userVO==null){
            return new JsonResult(Status.NOT_LOGIN);
        }
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO,article);
        article.setUserId(userVO.getId());
        articleMapper.insert(article);
        return JsonResult.success();
    }

    @GetMapping("/selectByAricleId")
    @ApiOperation("查询文章")
    @ApiImplicitParam(value = "文章id",name = "id",required = true,example = "1",dataType = "int")
    public JsonResult selectByAricleId(@Range(min = 1,max = 100,message = "文章id必须在1-100之间") Integer id){
        return JsonResult.success(articleMapper.selectById(id));
    }
}
